package com.examination.to.controller;

import com.examination.to.entity.User;
import com.examination.to.service.UserService;

import com.examination.to.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "登录注册")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return Result
     */
    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestParam(value = "username")String username,
                           @RequestParam(value = "password")String password){

        User user= new User();
        user.setUsername(username);
        user.setPassword(password);
        Result register = userService.register(user);
        log.info(user.toString());
        return register;
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return Result
     */
    @ApiOperation("用户登录接口")
    @GetMapping("/login")
    public Result login(@RequestParam(value = "username")String username,
                        @RequestParam(value = "password")String password) {
        User user= new User();
        user.setUsername(username);
        user.setPassword(password);
        log.info(user.toString());
        return userService.login(user);
    }

    @ApiOperation("测试连接")
    @GetMapping("/test")
    public List<User> findAll(){
        return userService.list();
    }


}
